package com.shopcart.controller;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.shopcart.entity.Product;

@Stateless(name = "ItemControllerBean", mappedName = "ejb/ItemControllerBean")
public class ItemControllerBean implements ItemController {

	@PersistenceContext
	private EntityManager em;

	public ItemControllerBean() {
	}

	@Override
	public void saveProduct(String productName, int productQuantity) {
		Product p = new Product();
		p.setName(productName);
		p.setQuantity(productQuantity);
		em.persist(p);
	}
	
	@Override
	public void updateProduct(Long productId, Integer count) {
		Query query = em.createQuery("select p from Product p where p.id=:id");
		query.setParameter("id", productId);
		Product product = (Product) query.getSingleResult();
		
		if (product != null) {
			product.setQuantity(count);
			em.merge(product);
		}
	}

	@Override
	public void matchProducts(String p1Str, String p2Str) {
		Query query = em.createQuery("select distinct p from Product p left join fetch p.matchingProducts where p.name=:name");
		query.setParameter("name", p1Str);
		Product p1 = (Product) query.getSingleResult();
		
		query = em.createQuery("select distinct p from Product p left join fetch p.matchingProducts where p.name=:name");
		query.setParameter("name", p2Str);
		Product p2 = (Product) query.getSingleResult();
		
		List<Product> p1List = p1.getMatchingProducts();
		List<Product> p2List = p2.getMatchingProducts();
		
//		if(! p1List.contains(p2))
//			p1List.add(p2);
//		if(! p2List.contains(p1))
//			p2List.add(p1);
		
		boolean contains1 = false, contains2 = false;
		
		for(Product p : p1List)
			if(p.getId() == p2.getId())
				contains1 = true;
		for(Product p : p2List)
			if(p.getId() == p1.getId())
				contains2 = true;
		if(! contains1)
			p1List.add(p2);
		if(! contains2)
			p2List.add(p1);
		
		
		
		em.merge(p1);
		em.merge(p2);
	}
	
	@Override
	public void unmatchProducts(String p1Str, String p2Str) {
		Query query = em.createQuery("select p from Product p left join fetch p.matchingProducts where p.name=:name");
		query.setParameter("name", p1Str);
		Product p1 = (Product) query.getSingleResult();
		
		query = em.createQuery("select p from Product p left join fetch p.matchingProducts where p.name=:name");
		query.setParameter("name", p2Str);
		Product p2 = (Product) query.getSingleResult();
		
		List<Product> p1List = p1.getMatchingProducts();
		List<Product> p2List = p2.getMatchingProducts();
		p1List.remove(p2);
		p2List.remove(p1);
		em.merge(p1);
		em.merge(p2);
	}

	@Override
	public List<Product> getProducts() {
		Query query = em.createQuery("select distinct p from Product p left join fetch p.matchingProducts");
		List<Product> products = (List<Product>) query.getResultList();
		return products;
	}
}